<?php
class Product_model_product extends Model {
    private $table_product = 'product';
    private $table_image = 'n_image';
    
    public function get_content($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        $db = $this->db;
        $product = $db->select_row(
            $this->table_product,
            array(),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
        
        if ( ! $product) return array();
        
        $images = $db->select_rows(
            $this->table_image,
            array(),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
        
        foreach ($images as $img) {
            $new_imgs[$img['key']][$img['id']] = $img;
        }
        $product['images'] = $new_imgs;
        return $product;
    }
    
    
    public function edit_content($node_id, $data) {
        
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        $db = $this->db;
        $table_product = $this->table_product;
        $table_image = $this->table_image;
        
        //$content = isset($data['content']) ? trim($data['content']) : '';
        
        $row = $db->select_row(
            $table_product,
            array('id'),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
        $id = 0;
        if ($row) {
            $id = (int) $row['id'];
            if ($id <= 0) tos_die();
            $delete_image_ids = isset($data['delete_image_ids']) ? 
                $data['delete_image_ids'] : array();
            if ($delete_image_ids) {
                $image_ids = array();
                foreach ($delete_image_ids as $image_id) {
                    $image_id = (int) $image_id;
                    if ($image_id > 0) {
                        $image_ids[] = $image_id;
                    }
                }
                if ($image_ids) {
                    $image_ids_sql = '(' . implode(',', $image_ids) . ')';
                    $sql = "DELETE FROM `{$table_image}` WHERE `id` in {$image_ids_sql}";
                    $db->execute($sql);
                }
            }
            
            if (isset($data['content'])) {
                $db->update(
                    $table_product, 
                    array('content' => trim($data['content'])), 
                    array('id' => $id)
                );
            }
        } else {
            if ( ! isset($data['content'])) tos_die();
            $content = trim($data['content']);
            $id = $db->insert(
                $table_product,
                array('node_id' => $node_id, 'content' => $content),
                array('node_id' => DB_DATA_INT)
            );
        }
        if (isset($data['src'])) {
            $srcs = $data['src'];
            $this->_add_images($node_id, $srcs);
        }
            
        return $id;
    }
    
    public function remvoe_content($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        return $this->db->delete($this->table_product, array('node_id' => $node_id));
    }
    
    private function _add_images($node_id, $srcs) {
        if ( ! $srcs) return ;
        foreach ($srcs as $key => $sub_srcs) {
            foreach ($sub_srcs as $src) {
                $this->db->insert(
                    $this->table_image, 
                    array('key' => $key, 'src' => $src, 'node_id' => $node_id)
                );
            }
        }
    }
}
?>